<!--
 *Author：jxx
 *Date：{Date}
 *Contact：283591387@qq.com
 *业务请在@/extension/mes/mes/ProcessOutsourceSettlement.jsx或ProcessOutsourceSettlement.vue文件编写
 *新版本支持vue或【表.jsx]文件编写业务,文档见:https://doc.volcore.xyz/docs/view-grid、https://doc.volcore.xyz/docs/web
 -->
<template>
    <div class="grid">
        <view-grid ref="grid" :columns="columns" :detail="detail" :details="details" :editFormFields="editFormFields"
            :editFormOptions="editFormOptions" :searchFormFields="searchFormFields" :searchFormOptions="searchFormOptions"
            :table="table" :extend="extend" :onInit="onInit" :onInited="onInited" :searchBefore="searchBefore"
            :searchAfter="searchAfter" :addBefore="addBefore" :updateBefore="updateBefore" :rowClick="rowClick"
            :modelOpenBefore="modelOpenBefore" :modelOpenAfter="modelOpenAfter">
            <!-- 自定义组件数据槽扩展，更多数据槽slot见文档 -->
            <template #gridHeader>
            </template>
        </view-grid>
    </div>
    <select-supply ref="supplyRef" @onSelectSupply="onSelectSupply"></select-supply>
</template>
<script setup lang="jsx">
import extend from "@/extension/mes/mes/ProcessOutsourceSettlement.jsx";
import viewOptions from './ProcessOutsourceSettlement/options.js'
import SelectSupply from './ProcessOutsourceSettlementSelectSupply.vue'
import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
const grid = ref(null);
const { proxy } = getCurrentInstance()
//http请求，proxy.http.post/get
const { table, editFormFields, editFormOptions, searchFormFields, searchFormOptions, columns, detail, details } = reactive(viewOptions())

let gridRef;//对应[表.jsx]文件中this.使用方式一样
const supplyRef = ref();
//生成对象属性初始化
const onInit = async ($vm) => {
    gridRef = $vm;
    //与jsx中的this.xx使用一样，只需将this.xx改为gridRef.xx
    //更多属性见：https://doc.volcore.xyz/docs/view-grid
    const selectSupplyOption = gridRef.getFormOption("SupplyName");
    selectSupplyOption.readonly = true;
    selectSupplyOption.extra = {
        icon: 'el-icon-zoom-out', //显示图标
        text: '选择', //显示文本
        style: 'color: #3a8ee6;font-size: 13px;cursor: pointer;',
        //触发事件
        click: (item) => {
            supplyRef.value.open();
        }
    };
}
//生成对象属性初始化后,操作明细表配置用到
const onInited = async () => {
    columns.forEach(x => {
        //开启表头过滤
        x.filterData = true;
    })

    gridRef.detailOptions.columns.forEach((col) => {
        //给数量Qty字段合计自定义显示格式、同时与表单联动显示
        if (col.field == 'SettlementAllAmount') {
            //将eidt设置为null不开启编辑
            col.edit = null;
            col.formatter = (row) => {
                row.SettlementPrice = (row.SettlementAllPrice || 0) * (1 - (row.Rate || 0) / 100)
                row.SettlementAmount = (row.SettlementPrice || 0) * (row.SettlementQty || 0)
                row.RateAmount = (row.SettlementAllPrice || 0) * (row.SettlementQty || 0) * ((row.Rate || 0) / 100)
                return (row.SettlementAllPrice || 0) * (row.SettlementQty || 0)
            }
        }
    });
}
const searchBefore = async (param) => {
    //界面查询前,可以给param.wheres添加查询参数
    //返回false，则不会执行查询
    return true;
}
const searchAfter = async (rows, result) => {
    return true;
}
const addBefore = async (formData) => {
    //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
    return true;
}
const updateBefore = async (formData) => {
    //编辑保存前formData为对象，包括明细表、删除行的Id
    return true;
}
const rowClick = ({ row, column, event }) => {
    //查询界面点击行事件
    // grid.value.toggleRowSelection(row); //单击行时选中当前行;
}
const modelOpenBefore = async (row) => {//弹出框打开后方法
    return true;//返回false，不会打开弹出框
}
const modelOpenAfter = (row) => {

    gridRef.setFormReadonly(false);

    if (gridRef.currentAction == 'Add') {
        gridRef.editFormFields.BillDate = proxy.base.getDate();
        gridRef.boxButtons.forEach((btn) => {
            if (btn.name == '保存') {
                btn.hidden = false;

            }

        });
    }
    else {
        if (row.AuditStatus != 0) {
            gridRef.setFormReadonly(true);
            gridRef.boxButtons.forEach((btn) => {
                if (btn.name == '保存') {
                    btn.hidden = true;
                    //或者设置只读
                    // btn.readonly = true;
                }

            });
        }
        else {
            gridRef.boxButtons.forEach((btn) => {
                if (btn.name == '保存') {
                    btn.hidden = false;
                    //或者设置只读
                    // btn.readonly = true;
                }
            });

        }
    }

    detail.columns.forEach(c => {
        if (c.field == 'MaterialNo' || c.field == 'MaterialName'
            || c.field == 'Spec' || c.field == 'UnitId'
            || c.field == 'RateAmount' || c.field == 'OutQty') {
            c.checkEdit = (row, column, index) => {
                //可根据row.字段的值返回true/false是否可以编辑
                return false
            }
        }

    })

}
const onSelectSupply = (rows) => {
    editFormFields.SupplyId = rows[0].SupplyId;
    editFormFields.SupplyName = rows[0].Name;
}
//监听表单输入，做实时计算
//watch(() => editFormFields.字段,(newValue, oldValue) => {	})
//对外暴露数据
defineExpose({})
</script>
